package com.claudiushauptmann.gwt.recaptcha.sample.client;

import com.claudiushauptmann.gwt.recaptcha.client.RecaptchaWidget;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class RecaptchaTest implements EntryPoint {
	private RecaptchaWidget rw;

	public void onModuleLoad() {
		rw = new RecaptchaWidget("6LeoPwYAAAAAAEgl-99fWvVvzRQObu5UoTPoQtg1");
		RootPanel.get().add(rw);

		Button btnVerify = new Button("Verify");
		btnVerify.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RecaptchaRemoteServiceAsync rrsa = RecaptchaRemoteService.Util
						.getInstance();
				rrsa.verifyChallenge(rw.getChallenge(), rw.getResponse(),
						new AsyncCallback<Boolean>() {

							public void onFailure(Throwable caught) {
								Window.alert("ERROR");
							}

							public void onSuccess(Boolean result) {
								Window.alert(result.toString());
							}

						});
			}
		});
		RootPanel.get().add(btnVerify);

		Button btnReload = new Button("Reload");
		btnReload.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				rw.reload();
			}
		});
		RootPanel.get().add(btnReload);

		Button btnChallenge = new Button("Challenge");
		btnChallenge.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.alert(rw.getChallenge());
			}
		});
		RootPanel.get().add(btnChallenge);

		Button btnResponse = new Button("Response");
		btnResponse.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				Window.alert(rw.getResponse());
			}
		});
		RootPanel.get().add(btnResponse);

		Button btnFocusResponseField = new Button("FocusResponseField");
		btnFocusResponseField.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				rw.focusResponseField();
			}
		});
		RootPanel.get().add(btnFocusResponseField);

		Button btnShowHelp = new Button("ShowHelp");
		btnShowHelp.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				rw.showHelp();
			}
		});
		RootPanel.get().add(btnShowHelp);

		Button btnSwitchType = new Button("SwitchType");
		btnSwitchType.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				rw.switchType("audio");
			}
		});
		RootPanel.get().add(btnSwitchType);
	}

}
